package org.example.day20240903文件操作.worldCup;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class Main {
    public static void main(String[] args) throws IOException {
        FileReader fileReader = new FileReader("worldcup.txt");
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        bufferedReader.readLine();
        bufferedReader.readLine();
        String line = bufferedReader.readLine();
        Map<String,String> worldCups = new HashMap<>();
        while(line != null){
            String[] arr = line.split(" ");
            worldCup worldCup = new worldCup(arr[0],arr[1],arr[2],arr[3]);
            if(worldCups.containsKey(worldCup.country)){
                if(Integer.parseInt(worldCups.get(worldCup.country).substring(0,4))
                        -Integer.parseInt(worldCup.year.substring(0,4)) > 0){
                    worldCups.put(worldCup.country,worldCup.year);
                }
                line = bufferedReader.readLine();
                continue;
            }
            worldCups.put(worldCup.country,worldCup.year);
            line = bufferedReader.readLine();
        }
        FileWriter fileWriter1 = new FileWriter("worldcupoutput.txt");
        BufferedWriter outBufferedWriter = new BufferedWriter(fileWriter1);
        for (Map.Entry<String,String> entry : worldCups.entrySet()){
            String year = entry.getValue().substring(0,4);
            outBufferedWriter.write(entry.getKey()+year+"\n");
        }
        outBufferedWriter.close();

    }
}
